use actix_web::{post, web::{scope, ServiceConfig}};
use serde::Deserialize;

use crate::{store::user::{self, User}, web::codec::{cbor::{new_ok_cbor, Cbor, CborRet}, user_session::save_session}};

#[derive(Debug, Deserialize)]
struct LoginReq {
    account: String,
    pwd: String,
}

#[post("/login")]
async fn on_login(req: Cbor<LoginReq>) -> CborRet<User> {
    let user = user::login(&req.account, &req.pwd).await?;
    save_session(user.id);
    new_ok_cbor(user)
}

pub fn register(cfg: &mut ServiceConfig) {
    let scope = scope("/user")
        .service(on_login)
    ;
    cfg.service(scope);
}

